package com.c.yinyuezhushou.Fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.c.yinyuezhushou.Adapter.LyricRecAdapter;
import com.c.yinyuezhushou.MRetrofit.MyObserver;
import com.c.yinyuezhushou.OnScrollListener.LyricRecOnScrollListener;
import com.c.yinyuezhushou.R;
import com.c.yinyuezhushou.MRetrofit.GetLyric;
import com.c.yinyuezhushou.MRetrofit.MusicItem;
import com.c.yinyuezhushou.MRetrofit.RetrofitService;
import com.c.yinyuezhushou.MRetrofit.MyRetrofit;
import com.c.yinyuezhushou.Ui.PlayerActivity;

import java.util.List;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.functions.Consumer;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class LyricsFragment extends Fragment {
     private View rootView;
     private MusicItem musicItem;
     private RecyclerView recyclerView;
     private List<Long> list;
     private TextView textView;
     private RelativeLayout relativeLayout;
     private ImageView imageView;
     private ProgressBar progressBar;
     private LinearLayoutManager linearLayoutManager;
     private LyricRecAdapter lyricRecAdapter;
     private LyricRecOnScrollListener lyricRecOnScrollListener;
     private boolean sliding = false;
     private int height =0;
     private long duration;
     private boolean new1=false;
     private int cz;

     public int getCz() {
          return cz;
     }

     public List<Long> getList() {
          return list;
     }

     public LyricsFragment(MusicItem musicItem, long duration){
          this.musicItem = musicItem;
          this.duration = duration;
     }

     public RelativeLayout getRelativeLayout() {
          return relativeLayout;
     }

     public ImageView getImageView() {
          return imageView;
     }

     public ProgressBar getProgressBar() {
          return progressBar;
     }

     public long getDuration() {
          return duration;
     }

     public void setSliding(boolean sliding) {
          this.sliding = sliding;
     }

     public boolean isSliding() {
          return sliding;
     }

     @Nullable
     @Override
     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
          rootView = inflater.inflate(R.layout.fragment_lyrics, container, false);
          recyclerView = rootView.findViewById(R.id.lyrics_rec);
          relativeLayout = rootView.findViewById(R.id.lyrics_re);
          imageView = rootView.findViewById(R.id.lyrics_image);
          progressBar = rootView.findViewById(R.id.lyrics_pro);
          return rootView;
     }

     @Override
     public void onActivityCreated(@Nullable Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);

          setRec();

     }

     public void setRec(){
          linearLayoutManager = new LinearLayoutManager(getActivity());
          recyclerView.setLayoutManager(linearLayoutManager);
          progressBar.setMax((int) duration);
          MyRetrofit.getOb(LyricsFragment.this.getContext(), RetrofitService.class).getLyric(musicItem.getId())
                  .subscribeOn(Schedulers.io())
                  .observeOn(AndroidSchedulers.mainThread())
                  .subscribe(new MyObserver<GetLyric>() {
                       @Override
                       public void onNext(@io.reactivex.rxjava3.annotations.NonNull GetLyric getLyric) {
                            String s = getLyric.getLrc().getLyric();
                            lyricRecAdapter = new LyricRecAdapter(s,LyricsFragment.this);
                            recyclerView.setAdapter(lyricRecAdapter);
                            list = lyricRecAdapter.getList();
                            lyricRecOnScrollListener = new LyricRecOnScrollListener(LyricsFragment.this,linearLayoutManager);
                            recyclerView.addOnScrollListener(lyricRecOnScrollListener);
                            imageView.setOnClickListener(new View.OnClickListener() {
                                 @Override
                                 public void onClick(View view) {
                                      PlayerActivity activity = (PlayerActivity) getActivity();
                                      activity.setSchedule(progressBar.getProgress());
                                 }
                            });
                       }

                       @Override
                       public void onError(@io.reactivex.rxjava3.annotations.NonNull Throwable e) {
                            Toast.makeText(getContext(), "歌词获取错误", Toast.LENGTH_SHORT).show();
                       }
                  });

     }


     public RecyclerView getRecyclerView() {
          return recyclerView;
     }

     public void setRec(MusicItem musicItem,long duration){
          this.duration = duration;
          this.musicItem = musicItem;
          new1=false;
          setRec();
     }
     public void scroll(long hm){
          if (list!=null){
               if (textView!=null){
                    textView.setTextSize(15);
                    textView.setTextColor(getResources().getColor(R.color.bai1));
               }

               if (!new1){
                    if (linearLayoutManager.findViewByPosition(linearLayoutManager.findFirstCompletelyVisibleItemPosition())!=null){
                         height = linearLayoutManager.findViewByPosition(linearLayoutManager.findFirstCompletelyVisibleItemPosition()).getHeight();
                         int recheight = recyclerView.getHeight();
                         cz = recheight / height / 2;
                         lyricRecAdapter.getcz(cz);
                         if (cz!=0){list = lyricRecAdapter.getList();
                         if (lyricRecAdapter.isNew1()){
                              new1=true;
                         }
                         }
                    }
               }

               Long l = null;
               for (Long l1:list){
                    if (hm>=l1){
                         l=l1;
                    }
               }

               if (!sliding){
                    if (cz!=0){
                         scroll(list.indexOf(l) + cz);
                         setText(list.indexOf(l));
                    }
               }
          }


     }
     public void setText(int i){
          if (linearLayoutManager.findViewByPosition(i)!=null){
               textView = linearLayoutManager.findViewByPosition(i).findViewById(R.id.ilr_text);
               textView.setTextSize(18);
               textView.setTextColor(getResources().getColor(R.color.bai));
          }
     }
     public void scroll(int pos){
          int top = linearLayoutManager.findFirstCompletelyVisibleItemPosition();
          int bootom = linearLayoutManager.findLastCompletelyVisibleItemPosition();

          if (top<= pos&&pos>=bootom){
               recyclerView.smoothScrollToPosition(pos);
          }else {
               recyclerView.smoothScrollBy(0,-(bootom-pos)* height);
          }
     }
     public int getBootom(){
          return linearLayoutManager.findLastCompletelyVisibleItemPosition();
     }

     @Override
     public void onDestroy() {
          super.onDestroy();
          if (lyricRecOnScrollListener!=null)
          lyricRecOnScrollListener.closehandle();
     }
}
